package bsc.war.form;



import bsc.ejb.entity.ColorimetrySettings;
import bsc.ejb.entity.Person;
import bsc.ejb.entity.PersonScoreCard;
import bsc.ejb.entity.ScoreCard;
import bsc.ejb.entity.ScoreCardRelation;
import bsc.ejb.entity.Settings;
import bsc.ejb.metadata.Colorimetry;
import bsc.ejb.metadata.Framework;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author memozac
 */
public class NewScoreCard extends AbstractController  {

     @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
         HttpServletResponse response) throws Exception {
        
        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");

            boolean edit = false;
            Long idScoreCard = -1L;

            if (NumberUtils.isDigits(request.getParameter("idScoreCard"))) {
                Long id = Long.valueOf(request.getParameter("idScoreCard"));
                if (id >= 0) {
                    edit = true;
                    idScoreCard = id;
                }
            }

            ScoreCard scoreCard = null;
            Settings scoreCardSettings = null;

            if(edit){
                scoreCard = delegate.findScoreCard(idScoreCard);
                scoreCardSettings = scoreCard.getSettings();
            }else{
                scoreCard = (ScoreCard) Factory.getBean(request.getParameter("type"));
                scoreCardSettings = (Settings) Factory.getBean("Settings");
            }

            
            //ScoreCardRelation scoreCardRelation = (ScoreCardRelation) Factory.getBean("ScoreCardRelation");

            //Long idScoreCard = request.getParameter("scoreCard") != null && NumberUtils.isNumber(request.getParameter("scoreCard")) ? Long.parseLong(request.getParameter("scoreCard")) : null;
            Long idPerson = request.getParameter("person") != null && NumberUtils.isNumber(request.getParameter("person")) ? Long.parseLong(request.getParameter("person")) : null;
            //ScoreCard parentScoreCard = delegate.findScoreCard(idScoreCard);

            //scorecard
            scoreCard.setDescription(request.getParameter("description"));
            scoreCard.setName(request.getParameter("name"));
            Person person = delegate.findPerson(idPerson);
            scoreCard.setResponsible(person);

            if(edit){
                delegate.updateScoreCard(scoreCard);
            }else{
                delegate.createScoreCard(scoreCard);
            }
            

            //personScoreCard
            /*PersonScoreCard personScoreCard = (PersonScoreCard) Factory.getBean("PersonScoreCard");
            Person person = delegate.findPerson(idPerson);
            personScoreCard.setPerson(person);
            personScoreCard.setScoreCard(scoreCard);
            delegate.createPersonScoreCard(personScoreCard);*/

            //scoreCard settings
            scoreCardSettings.setScoreCard(scoreCard);
            scoreCardSettings.setType(Settings.Type.SCORECARD);
            //parentScoreCard.setUnit(request.getParameter("description"));
            if(edit){
                delegate.updateSettings(scoreCardSettings);
            }else{
                delegate.createSettings(scoreCardSettings);
            }

            scoreCard.setSettings(scoreCardSettings);
            delegate.updateScoreCard(scoreCard);
            

            //scoreCardRelation
            //scoreCardRelation.setScoreCard(parentScoreCard);
            //scoreCardRelation.setOtherScoreCard(scoreCard);
            //delegate.createScoreCardRelation(scoreCardRelation);

            //colorimetries
            //remove old colorimetries
            List<ColorimetrySettings> colorimetriesSettings = delegate.findColorimetrySettings(scoreCard);
            for (ColorimetrySettings colorimetrySettings : colorimetriesSettings) {
                delegate.deleteColorimetrySettings(colorimetrySettings);
            }

            Framework framework = delegate.findFramework(Boolean.TRUE);
            List<Colorimetry> colorimetries = delegate.findColorimetry(framework, Boolean.TRUE);
            for (Colorimetry colorimetry : colorimetries) {
                ColorimetrySettings colorimetrySettings = (ColorimetrySettings) Factory.getBean("ColorimetrySettings");
                String minValue = request.getParameter("min_value_colorimetry_" + colorimetry.getIdColorimetry());
                String maxValue = request.getParameter("max_value_colorimetry_" + colorimetry.getIdColorimetry());
                Double maxDob= (NumberUtils.isNumber(maxValue) ? Double.parseDouble(maxValue) : Double.valueOf(maxValue))/100;
                Double minDob= (NumberUtils.isNumber(minValue) ? Double.parseDouble(minValue) : Double.valueOf(minValue))/100;
                colorimetrySettings.setMaxScore(maxDob);
                colorimetrySettings.setMinScore(minDob);
                colorimetrySettings.setSettings(scoreCardSettings);
                colorimetrySettings.setColorimetry(colorimetry);
                delegate.createColorimetrySettings(colorimetrySettings);
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Caught exception: "+e);
        }
         
         return new ModelAndView("newScoreCard");
    }

}
